#!/bin/bash

# 定义镜像和容器的名字
IMAGE_NAME="http-client"
CONTAINER_NAME="http-client"

# 停止并删除旧的容器
echo "Stopping container $CONTAINER_NAME..."
docker stop "$CONTAINER_NAME" || true
echo "Removing container $CONTAINER_NAME..."
docker rm "$CONTAINER_NAME" || true

# 删除旧的镜像
echo "Removing image $IMAGE_NAME..."
docker rmi "$IMAGE_NAME" || true

# 打包新的镜像
echo "Building new image $IMAGE_NAME..."
docker build -t "$IMAGE_NAME" .

# 运行新镜像
echo "Running new container $CONTAINER_NAME..."
docker run \
    -itd \
    --net=host \
    --restart always \
    --privileged \
    -d \
    -p 9999:9999 \
    --name "$CONTAINER_NAME" \
    "$IMAGE_NAME"

# 检查容器是否成功启动
if docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME"; then
    echo "Container $CONTAINER_NAME is running."
else
    echo "Failed to start container $CONTAINER_NAME."
fi
